package org.hbbTlias2025.aop;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.hbbTlias2025.mapper.OperateLogMapper;
import org.hbbTlias2025.pojo.OperateLog;
import org.hbbTlias2025.utils.CurrentHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;


@Slf4j
@Aspect
@Component
public class RecordTimeAspect {


    @Autowired
    private OperateLogMapper operateLogMapper;

    @Pointcut("execution(* org.hbbTlias2025.service.IMPL.*.*(..))")
    public void pt(){}

//    @Pointcut("execution(* org.example.hbbproject010303.service.IMPL.StudentServiceImpl.*(..))")
//    public void ptStudent(){}


    @Around("pt()")
    public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {
        long startTime = System.currentTimeMillis() ;


        Object result = pjp.proceed();
        String methodName = pjp.getSignature().getName() ;

        long endTime = System.currentTimeMillis() ;
        log.info("当前方法{}运行时间————{}ms",methodName, endTime - startTime);

        return result ;
    }

//    @Before("@annotation(org.example.hbbproject010303.anno.LogOpreation)")
    public void before(JoinPoint jp) {
        log.info("before....");
        log.info("获取目标对象：{}", jp.getTarget());
//        获取目标类
        log.info("获取目标类：{}", jp.getTarget().getClass().getName());
//        获取目标方法
        log.info("获取目标方法：{}", jp.getSignature().getName());
        log.info("获取目标方法参数：{}", jp.getArgs());
    }
    private Integer getCurrentId() {
        return CurrentHolder.getCurrentId();
    }

    @Around("@annotation(org.hbbTlias2025.anno.LogOpreation)")
    public Object studentOperationLog(ProceedingJoinPoint pjp) throws Throwable {

        try {
            OperateLog operateLog = new OperateLog();

            long startTime = System.currentTimeMillis() ;

            operateLog.setOperateTime(LocalDateTime.now());
            operateLog.setOperateEmpId(getCurrentId());
            operateLog.setClassName(pjp.getTarget().getClass().getName());
            operateLog.setMethodName(pjp.getSignature().getName());
            operateLog.setMethodParams(Arrays.toString(pjp.getArgs()));

            Object result = pjp.proceed();

            long endTime = System.currentTimeMillis() ;

            operateLog.setReturnValue(
                    result != null
                            ? (result.toString().length() > 1990
                            ? result.toString().substring(0, 1990)
                            : result.toString())
                            : "void"
            );


            operateLog.setCostTime(endTime - startTime);
            log.info("这是recordAspect当前方法{}运行时间————{}ms",operateLog.getMethodName(), endTime - startTime);


            operateLogMapper.insert(operateLog);

            return result;
        } catch (Throwable e) {
            log.error("记录操作日志失败+++{}", e.getMessage());
            throw e;
        }
    }
}
